package com.bootdo.wx.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.wx.dao.CmsContentDao;
import com.bootdo.wx.dao.MemberMamaDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberMamaDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberMamaService;
import com.github.pagehelper.PageInfo;



@Service
public class MemberMamaServiceImpl implements MemberMamaService {
	@Autowired
	private MemberMamaDao memberMamaDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private CmsContentDao cmsContentDao;
	
	@Override
	public MemberMamaDO get(String id){
		return memberMamaDao.get(id);
	}
	
	@Override
	public List<MemberMamaDO> list(Map<String, Object> map){
		return memberMamaDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberMamaDao.count(map);
	}
	
	@Override
	public int save(MemberMamaDO memberMama){
		return memberMamaDao.save(memberMama);
	}
	
	@Override
	public int update(MemberMamaDO memberMama){
		return memberMamaDao.update(memberMama);
	}
	
	@Override
	public int remove(String id){
		return memberMamaDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberMamaDao.batchRemove(ids);
	}

	@Override
	public List<MemberMamaDO> likeList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return memberMamaDao.likeList(map);
	}

	/*@Override
	public R getBaoMaManual(Map<String, Object> map) {
		if("-1".equals(map.get("userId").toString())){
			map.put("delFlag", "0");
			map.put("sort", "days");
			map.put("order", "asc");
			List<MemberMamaDO> list = memberMamaDao.list(map);
			return R.ok().put("data", list);
		}else{
			MemberUserDO memberUserDO = memberUserDao.get(map.get("userId").toString());
			if(memberUserDO==null){
				return R.error("用户信息有误");
			}else{
				//停经的日期
				String babyDays = memberUserDO.getBabyDays();
				Date stringToDate = DateUtils.getStringToDate(babyDays);
				//当前的日期减去停经日期的天数
				int diffDays = DateUtils.getDiffDays(new Date(),stringToDate);
				
			}
		}
		return null;
	}*/

	@Override
	public R getAllCycle(Map<String, Object> map) {
		Map<String, Object> returnMap = new HashMap<>();
		PageHelperUtils.page(map);
		List<String> list = memberMamaDao.getAllCycle(map);
		PageInfo<String> pageInfo = new PageInfo<>(list);
		returnMap.put("pageInfo", pageInfo);
		//获取用户今天的周期
		if("-1".equals(map.get("userId").toString())){
			returnMap.put("userCycle", "-1");
			return R.ok().put("data", returnMap);
		}else{
			//停经的日期
			MemberUserDO memberUserDO = memberUserDao.get(map.get("userId").toString());
			String babyDays = memberUserDO.getBabyDays();
			Date stringToDate = DateUtils.getStringToDate(babyDays);
			//当前的日期减去停经日期的天数
			int diffDays = DateUtils.getDiffDays(stringToDate,new Date());
			diffDays=diffDays/7;
			//用户当前的周期
			String userCycle = memberMamaDao.findByDays(diffDays);
			if(StringUtils.isEmpty(userCycle)){
				returnMap.put("userCycle", -1);
			}else{
				returnMap.put("userCycle", userCycle);
			}
			
			return R.ok().put("data", returnMap);
		}
		
	}

	@Override
	public R getBaoMaByCycle(Map<String, Object> map) {
		Map<String, Object> returnMap = new HashMap<>();
		if("-1".equals(map.get("userCycle").toString())){
			map.put("delFlag", "0");
			map.put("sort", "days");
			map.put("order", "asc");
			List<MemberMamaDO> list = memberMamaDao.list(map);
		
			returnMap.put("chages", list.get(0).getChages());
			returnMap.put("remind", list.get(0).getRemind());
			return  R.ok().put("data", returnMap);
		}else{
			map.put("name", map.get("userCycle").toString());
			map.put("delFlag", "0");
			List<MemberMamaDO> list = memberMamaDao.list(map);
			Integer days=0;
			if(list.size()>0){
				days = list.get(0).getDays();
			}else{
				days =1;
			}
			
			String qDay = memberMamaDao.findByDays(days-1);
			String hDay = memberMamaDao.findByDays(days+1);
			returnMap.put("qDay", qDay);
			returnMap.put("hDay", hDay);
			returnMap.put("dDay", map.get("userCycle").toString());
			returnMap.put("memberMamaList", list);
			
			return  R.ok().put("data", returnMap);
		}
	}
	
}
